// import Vue from 'vue';
// import Router from 'vue-router';



// import VueRouter from 'vue-router';
// import { resolve } from '../../build/webpack.base.conf'

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push (location) {
   return originalPush.call(this, location).catch(err => err);
};




const router = new VueRouter({
   routes: [
      {
         path: '/',
         name: '登录',
         hidden: true,
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Login'));
               },
               'login'
            )
      },
      {
         path: '/error',
         name: '404',
         hidden: true,
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/views/404'));
               },
               'error'
            )
      },
      {
         path: '/home',
         name: '',
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Home'));
               },
               'home'
            ),
         hidden: true
      },
      {
         path: '/home',
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Home'));
               },
               'home'
            ),
         name: '包间设置',
         iconCls: 'el-icon-s-platform',
         children: [
            {
               path: '/tollRule',
               iconCls: 'el-icon-s-platform',
               name: '计费规则配置',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/tabSet/tollRule'));
                     },
                     'tollRule'
                  ),
               meta: {
                  keepAlive: true
               }
            },
            {
               path: '/tab',
               iconCls: 'el-icon-s-platform',
               name: '包间配置',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/tabSet/tabSet'));
                     },
                     'tab'
                  ),
               meta: {
                  keepAlive: true
               }
            },
            {
               path: '/privateRoomCategory',
               iconCls: 'el-icon-s-platform',
               name: '包间类别',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/tabSet/privateRoomCategory'));
                     },
                     'privateRoomCategory'
                  ),
               meta: {
                  keepAlive: true
               }
            }
         ]
      },
      {
         path: '/home',
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Home'));
               },
               'home'
            ),
         name: '日志管理',
         iconCls: 'el-icon-s-claim',
         children: [
            {
               path: '/log',
               iconCls: 'el-icon-s-claim',
               name: '系统操作日志',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/logTab/LogList'));
                     },
                     'log'
                  ),
               meta: {
                  keepAlive: true
               }
            }
         ]
      },
      {
         path: '/home',
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Home'));
               },
               'home'
            ),
         name: '前台收银',
         iconCls: 'el-icon-shopping-cart-full',
         children: [
            {
               path: '/register',
               iconCls: 'el-icon-shopping-cart-full',
               name: '前台收银',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/register/register'));
                     },
                     'register'
                  ),
               meta: {
                  keepAlive: true
               }
            }
         ]
      },
      {
         path: '/home',
         component: resolve =>
            require.ensure(
               [],
               function (require) {
                  resolve(require('@/components/Home'));
               },
               'home'
            ),
         name: '商品管理',
         iconCls: 'el-icon-s-goods',
         children: [
            {
               path: '/product',
               iconCls: 'el-icon-s-goods',
               name: '商品资料',
               component: resolve =>
                  require.ensure(
                     [],
                     function (require) {
                        resolve(require('@/views/product/productInfo'));
                     },
                     'product'
                  ),
               meta: {
                  keepAlive: true
               }
            }
         ]
      }
   ],
});

var vm = new Vue;

function checkPermission (menuInfo, location) {
   // 遍历数组对象
   for (let i = 0; i < menuInfo.length; i++) {
      const item = menuInfo[i];
      // 判断是否存在子节点
      if (item.children && item.children.length) {
         const hasPermissionInChildren = checkPermission(
            item.children,
            location
         );
         // 如果子节点中有某个节点有权限，则当前节点也有权限
         if (hasPermissionInChildren) {
            return true;
         }
      } else {
         // 没有子节点，判断该节点的path是否与location匹配
         if (item.path === location) {
            // 有权限则返回true
            return true;
         }
      }
   }
   // 遍历完所有节点，没有匹配到则返回false
   return false;
}

router.beforeEach((to, from, next) => {
   const menuInfo = vm.$util.getLocalStorage('menuInfo');
   const nowPath = to.path;
   const publicPaths = ['/', '/home', '/error', '/ip', '/aes'];
   if (publicPaths.includes(nowPath)) {
      next();
      return;
   }

   // 如果没有登录信息，则跳转到首页
   if (!menuInfo) {
      next('/');
      return;
   }

   // 如果有登录信息，则检查当前页面是否有访问权限
   if (checkPermission(menuInfo, nowPath)) {
      next();
   } else {
      next('/error');
   }

});

export default router;
